# == Schema Information
#
# Table name: answers
#
#  id             :integer(4)      not null, primary key
#  person_id      :integer(4)      not null
#  question_id    :integer(4)      not null
#  answer_id      :integer(1)
#  correct_answer :integer(1)
#  created_at     :datetime
#  updated_at     :datetime
#  request_ip     :string(255)
#  user_agent     :string(255)
#
# Indexes
#
#  index_answers_on_person_id_and_question_id  (person_id,question_id)
#

class Answer < ActiveRecord::Base
  validates :person_id, :question_id, :answer_id, :presence => true
  validates_uniqueness_of :question_id, :scope => :person_id
  
  belongs_to :person, :counter_cache => true
  belongs_to :question
  
  #scope :by_people, group("answers.person_id")
  
  before_save :set_correct_answer
  
  def is_correct?
    self.answer_id == self.correct_answer
  end
  
  private
  
    def set_correct_answer
      self.correct_answer = question.correct_answer
    end
end
